home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue54 / Persist / FEditPerson.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-23  |  9.3 KB  |  311 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   (c) TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: Jan 2000
  10.  
  11.   Notes: Dialog for editing a person's details and browsing their
  12.          Addresses and EAddresses. This dialog is used as the LH
  13.          pane of the main form and is controled by the TtiTreeViewPlus.
  14.  
  15. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  16. unit FEditPerson;
  17.  
  18. interface
  19.  
  20. uses
  21.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  22.   Adrs_BOM, ComCtrls, tiListView, StdCtrls, FEditEAddress
  23.   ,FEditAddress
  24.   ;
  25.  
  26. type
  27.   TFormEditPerson = class(TForm)
  28.     GroupBox1: TGroupBox;
  29.     eLastName: TEdit;
  30.     eFirstName: TEdit;
  31.     eInitials: TEdit;
  32.     cbTitle: TComboBox;
  33.     Label1: TLabel;
  34.     Label2: TLabel;
  35.     Label3: TLabel;
  36.     Label4: TLabel;
  37.     GroupBox2: TGroupBox;
  38.     GroupBox3: TGroupBox;
  39.     LVAddress: TtiListView;
  40.     GroupBox4: TGroupBox;
  41.     mNotes: TMemo;
  42.     LVEAddress: TtiListView;
  43.     procedure eLastNameChange(Sender: TObject);
  44.     procedure LVEAddressDelete(Sender: TObject);
  45.     procedure LVEAddressEdit(Sender: TObject);
  46.     procedure LVEAddressNew(Sender: TObject);
  47.     procedure FormCreate(Sender: TObject);
  48.     procedure FormDestroy(Sender: TObject);
  49.     procedure LVEAddressFilterData(pData: TPersistent; var pbInclude: Boolean);
  50.     procedure LVAddressEdit(Sender: TObject);
  51.     procedure LVAddressNew(Sender: TObject);
  52.     procedure LVAddressDelete(Sender: TObject);
  53.     procedure FormShow(Sender: TObject);
  54.   private
  55.     FData     : TPerson ;
  56.     FTreeNode : TTreeNode;
  57.     FEditEAddress : TFormEditEAddress ;
  58.     FEditAddress  : TFormEditAddress ;
  59.     procedure SetData(const Value: TPersistent);
  60.     function  GetData : TPersistent;
  61.     function  GetValid: boolean;
  62.   published
  63.     // These published properties are required by the TtiTreeViewPlus
  64.     property Data : TPersistent read GetData write SetData ;
  65.     property TreeNode : TTreeNode read FTreeNode write FTreeNode ;
  66.     property Valid    : boolean   read GetValid ;
  67.   public
  68.  
  69.   end;
  70.  
  71. implementation
  72. uses
  73.   tiPerObjAbs
  74.   ;
  75.  
  76. {$R *.DFM}
  77.  
  78. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  79. // *
  80. // * TFormEditPerson
  81. // *
  82. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  83. // Form's OnCreate event
  84. procedure TFormEditPerson.FormCreate(Sender: TObject);
  85. begin
  86.   FEditEAddress := TFormEditEAddress.Create( nil ) ;
  87.   FEditAddress  := TFormEditAddress.Create(  nil ) ;
  88. end ;
  89.  
  90. // Form's OnDelete event
  91. //------------------------------------------------------------------------------
  92. procedure TFormEditPerson.FormDestroy(Sender: TObject);
  93. begin
  94.   FEditEAddress.Free ;
  95.   FEditAddress.Free ;
  96. end ;
  97.  
  98. // Form's OnShow event
  99. //------------------------------------------------------------------------------
  100. procedure TFormEditPerson.FormShow(Sender: TObject);
  101. begin
  102.   if FData.ObjectState = posCreate then
  103.     eLastName.SetFocus ;
  104. end;
  105.  
  106. // Data's Get method
  107. //------------------------------------------------------------------------------
  108. function TFormEditPerson.GetData: TPersistent;
  109. begin
  110.   result := FData ;
  111. end;
  112.  
  113. // Data's Set method. Write data to the appropriate GUI controls.
  114. //------------------------------------------------------------------------------
  115. procedure TFormEditPerson.SetData(const Value: TPersistent);
  116. begin
  117.   if Value = nil then begin
  118.     FData := nil ;
  119.     exit ; //=>
  120.   end ;
  121.  
  122.   FData := Value as TPerson ;
  123.  
  124.   eLastName.Text    := FData.LastName ;
  125.   eFirstName.Text   := FData.FirstName ;
  126.   eInitials.Text    := FData.Initials ;
  127.   cbTitle.ItemIndex := cbTitle.Items.IndexOf( FData.Title ) ;
  128.   mNotes.Lines.Text := FData.Notes ;
  129.  
  130.   LVEAddress.Data   := FData.EAddressList.List ;
  131.   LVAddress.Data    := FData.AddressList.List ;
  132.  
  133. end;
  134.  
  135. // The LastName was changed, so change the treeNode's text
  136. //------------------------------------------------------------------------------
  137. procedure TFormEditPerson.eLastNameChange(Sender: TObject);
  138. begin
  139.   if FData = nil then
  140.     exit ; //==>
  141.   if FTreeNode = nil then
  142.     exit ; //==>
  143.   if not Valid then
  144.     exit ; //==>
  145.  
  146.   FTreeNode.Text := FData.Caption ;
  147.  
  148. end;
  149.  
  150. // The form's Valid property is called by the TtiTreeViewPlus before allowing
  151. // the node to change. Form validation is performed here.
  152. //------------------------------------------------------------------------------
  153. function TFormEditPerson.GetValid: boolean;
  154. begin
  155.   result := true ;
  156.   if FData = nil then
  157.     exit ; //==>
  158.  
  159.   // Has there been a change made ?
  160.   if ( FData.LastName    = eLastName.Text    ) and
  161.      ( FData.FirstName   = eFirstName.Text   ) and
  162.      ( FData.Initials    = eInitials.Text    ) and
  163.      ( FData.Title       = cbTitle.Items[ cbTitle.ItemIndex ] ) and
  164.      ( FData.Notes       = mNotes.Lines.Text ) then
  165.     exit ;
  166.  
  167.   // Check for a valid lastName
  168.   if eLastName.Text = '' then begin
  169.     MessageDlg( 'Please enter a last name',
  170.                 mtInformation,
  171.                 [mbOK], 0 ) ;
  172.     eLastName.SetFocus ;
  173.     result := false ;
  174.     exit ; //==>
  175.   end ;
  176.  
  177.   // Assign data from the form to the data object
  178.   FData.LastName    := eLastName.Text    ;
  179.   FData.FirstName   := eFirstName.Text   ;
  180.   FData.Initials    := eInitials.Text    ;
  181.   FData.Title       := cbTitle.Items[ cbTitle.ItemIndex ] ;
  182.  
  183.   // This requires work, as it should be its own object
  184.   FData.Notes       := mNotes.Lines.Text ;
  185.  
  186.   // Set the data objects ObjectState property to reflect the change.
  187.   //FData.ObjectState := posUpdate ;
  188.   FData.Dirty := true ;
  189.  
  190. end;
  191.  
  192. // TitListViewPlus action to delete an e-address.
  193. //------------------------------------------------------------------------------
  194. procedure TFormEditPerson.LVEAddressDelete(Sender: TObject);
  195. var
  196.   lData : TEAddress ;
  197. begin
  198.   if sender = nil then
  199.     exit ;
  200.   if TListItem( sender ).Data = nil then
  201.     exit ;
  202.  
  203.   lData := TEAddress( TListItem( Sender ).Data ) ;
  204.  
  205.   if MessageDlg( 'Are you sure you want to delete <' +
  206.                  lData.EAdrsType + ' - ' +
  207.                  lData.Text + '> ?',
  208.                  mtConfirmation,
  209.                  [mbYes, mbNo],
  210.                  0 ) = mrYes then begin
  211.     lData.Deleted := true ;
  212.     LVEAddress.Refresh ;
  213.   end ;
  214. end;
  215.  
  216. // TitListViewPlus action to edit an e-address
  217. //------------------------------------------------------------------------------
  218. procedure TFormEditPerson.LVEAddressEdit(Sender: TObject);
  219. begin
  220.  
  221.   if sender = nil then
  222.     exit ;
  223.   if TListItem( sender ).Data = nil then
  224.     exit ;
  225.  
  226.   // Sender is the data
  227.   FEditEAddress.Data := TEAddress( TListItem( Sender ).Data ) ;
  228.   if FEditEAddress.ShowModal = mrOK then begin
  229.     FEditEAddress.Data.ObjectState := posUpdate ;
  230.     LVEAddress.Refresh ;
  231.   end ;
  232.  
  233. end;
  234.  
  235. // TtiListViewPlus action to create a new e-address
  236. //------------------------------------------------------------------------------
  237. procedure TFormEditPerson.LVEAddressNew(Sender: TObject);
  238. begin
  239.   FEditEAddress.Data := TEAddress.CreateNew ;
  240.   if FEditEAddress.ShowModal = mrOK then begin
  241.     FData.EAddressList.Add( FEditEAddress.Data ) ;
  242.     FEditEAddress.Data.Owner := FData ;
  243.     LVEAddress.Refresh ;
  244.   end else
  245.     FEditEAddress.Data.Free ;
  246. end ;
  247.  
  248. // TtiListViewPlus on filter event to filter deleted e-addresses
  249. //------------------------------------------------------------------------------
  250. procedure TFormEditPerson.LVEAddressFilterData(pData: TPersistent; var pbInclude: Boolean);
  251. begin
  252.   pbInclude := not ( pData as TPerObjAbs ).Deleted ;
  253. end ;
  254.  
  255. // TtiListViewPlus action to edit an address
  256. //------------------------------------------------------------------------------
  257. procedure TFormEditPerson.LVAddressEdit(Sender: TObject);
  258. begin
  259.   if sender = nil then
  260.     exit ;
  261.   if TListItem( sender ).Data = nil then
  262.     exit ;
  263.  
  264.   // Sender is the data
  265.   FEditAddress.Data := TAddress( TListItem( Sender ).Data ) ;
  266.   if FEditAddress.ShowModal = mrOK then begin
  267.     FEditAddress.Data.ObjectState := posUpdate ;
  268.     LVEAddress.Refresh ;
  269.   end ;
  270.  
  271. end;
  272.  
  273. // TtiListViewPlus action to insert a new address
  274. //------------------------------------------------------------------------------
  275. procedure TFormEditPerson.LVAddressNew(Sender: TObject);
  276. begin
  277.   FEditAddress.Data := TAddress.CreateNew ;
  278.   if FEditAddress.ShowModal = mrOK then begin
  279.     FData.AddressList.Add( FEditAddress.Data ) ;
  280.     FEditAddress.Data.Owner := FData ;
  281.     LVAddress.Refresh ;
  282.   end else
  283.     FEditAddress.Data.Free ;
  284. end;
  285.  
  286. // TtiListViewPlus action to delete an address
  287. //------------------------------------------------------------------------------
  288. procedure TFormEditPerson.LVAddressDelete(Sender: TObject);
  289. var
  290.   lData : TAddress ;
  291. begin
  292.   if sender = nil then
  293.     exit ;
  294.   if TListItem( sender ).Data = nil then
  295.     exit ;
  296.  
  297.   lData := TAddress( TListItem( Sender ).Data ) ;
  298.  
  299.   if MessageDlg( 'Are you sure you want to delete <' +
  300.                  lData.AdrsType + ' - ' +
  301.                  lData.Text + '> ?',
  302.                  mtConfirmation,
  303.                  [mbYes, mbNo],
  304.                  0 ) = mrYes then begin
  305.     lData.Deleted := true ;
  306.     LVAddress.Refresh ;
  307.   end ;
  308. end;
  309.  
  310. end.
  311.